class events {
    constructor(props) {
        this.maxListeners = props.defaultMaxListeners ? props.defaultMaxListeners : 1;
        this.init();
    }
    init() {
        this._events = Object.create(null);
    }
    on(eventName, callback) {
        if (this._events[eventName]) {
            this._events[eventName].push(callback);
        } else {
            this._events[eventName] = [callback];
        }
        return this;
    }
    emit(eventName, ...args) {
        if (this._events[eventName]) {
            this._events[eventName].forEach((callback, index) => {
                if (index < this.maxListeners) callback.apply(this, args)
                else console.error(`最多只能监听${this.maxListeners}个事件`);
            });
        }
        return this;
    }
    removeListener(eventName, callback) {
        if (this._events[eventName]) {
            this._events[eventName] = this._events[eventName].filter(fn => fn !== callback);
        }
        return this;
    }
    once(eventName, callback) {

    }
    newListener(eventName, callback) {

    }
    currentListenerCount(eventName) {
        return this._events[eventName].length;
    }
    setMaxListeners(count) {
        this.maxListeners = count;
    }
    eventNames() {
        return Object.keys(this._events);
    }
    removeAllListeners(eventName) {
        if (this._events[eventName]) {
            delete this._events[eventName];
        } else {
            this.init();
        }
    }
}

const EventEmitter = new events({defaultMaxListeners: 2});

const buyHouse = (...args) => console.log(`buy houses ${args[0]}`);
const buyCars = (...args) => console.log(`buy cars ${args[0]}`);

EventEmitter.on('money', buyHouse).on('money', buyCars).emit('money', 'give my girl');